using System;
using System.Diagnostics;

namespace OfficeClip.OpenSource.Utility
{
 /// <summary>
 /// Summary description for Process.
 /// </summary>
 public class Process
 {
	 OpenSource.Utility.Log _log = null;

	 public Process(OpenSource.Utility.Log log)
	{
	 _log = log;
	}

	public Process()
	{

	}

	public int Run(string programPath, string processArgument)
	{
	 return Run(programPath, processArgument, true);
	}

	public int Run(string programPath, string processArgument, bool showOutput)
	{
	 int retVal = 0;
	 try
	 {
		string fullPath = string.Concat(programPath, " ", processArgument);
		if (_log != null) _log.Write(LogState.Info, "Run Program", fullPath);
		ProcessStartInfo processStartInfo = new ProcessStartInfo(programPath);
		processStartInfo.Arguments = processArgument;
		//processStartInfo.WindowStyle = ProcessWindowStyle.Minimized;
		processStartInfo.RedirectStandardError = true;
		processStartInfo.RedirectStandardOutput = true;
		processStartInfo.UseShellExecute = false;
		System.Diagnostics.Process process = System.Diagnostics.Process.Start(processStartInfo);
		//Utility.Constants.MimimizeWindow(process);
		string output = process.StandardOutput.ReadToEnd();
		string error = process.StandardError.ReadToEnd();
		process.WaitForExit();
		retVal = process.ExitCode;
		if ((output != "") && showOutput)
		 if (_log != null)
			_log.Write(LogState.Info, "Program Output", string.Concat(programPath, ": ", output));
		if (error != "")
		 if (_log != null)
			_log.Write(LogState.Error, "Program Error", string.Concat(programPath, ": ", error));
	 }
	 catch (Exception e)
	 {
		if (_log != null)
		 _log.Write(LogState.Error, "Run Program", "Failed to run: " + e.Message);
		retVal = -100;
	 }
	 return retVal;
	}

	public int Run(string dir, string programName, string processArgument)
	{
	 return Run(dir, programName, processArgument, true);
	}

	public int Run(string dir, string programName, string processArgument, bool showOutput)
	{
	 string str1;
	 str1 = string.Concat(dir, @"\", programName);
	 str1 = string.Concat(@"""", str1, @"""");
	 return Run(str1, processArgument, showOutput);
	}

 }
}
